package com.lebang.service;

import com.lebang.entity.sys.LbbRole;
import com.lebang.entity.sys.LbbUser;
import com.lebang.entity.sys.Resource;
import com.lebang.example.LbbRoleExample;
import com.lebang.mapper.*;
import com.lebang.model.enums.Booleans;
import com.lebang.model.vo.RoleVo;
import com.lebang.model.vo.SearchVo;
import com.lebangweb.common.TextUtil;
import com.lebangweb.common.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 角色
 *
 * @author weizx
 * @className RoleService
 * @date 2018年7月7日 下午4:15:49
 */
@Service
public class RoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Autowired
    private LbbAuthorityMapper authorityMapper;

    @Autowired
    private LbbRoleMapper lbbRoleMapper;

    @Value("#{'${check.account}'.split(',')}")
    private List<String> accounts;

    private List<String> urls = Arrays.asList("/sys/cencessdkerrorlog/list", "/sys/cencesadrecordlog/list", "/sys/cencesclickinfo/list","/sys/sdkerrorlog/list","/sys/aderrorconfig/list","/sys/cences/cencescoredata/list");

    @Autowired
    private LbbUserMapper lbbUserMapper;

    /**
     * 获取用户菜单
     *
     * @param userid
     * @return
     * @author weizx
     * @date 2018年7月9日 下午5:16:20
     */
    public List<Resource> getmenus(String userid) {
        // 获取用户角色
        List<String> userRoleList = userRoleMapper.getUserRole(userid);
        // 获取用户所在用户组角色
        List<String> userGroupRoleList = userGroupMapper.getUserGroupRole(userid);
        // 合并角色
        userRoleList.addAll(userGroupRoleList);
        // 获取角色菜单
        List<Resource> menus = authorityMapper.getMenus(userRoleList);
        for (Resource menu : menus) {
            // 获取角色菜单项
            List<Resource> items = authorityMapper.getMenuItems(userRoleList, menu.getResource());
            menu.setItems(items);
        }
        return menus;
    }

    /**
     * 角色列表
     *
     * @param vo
     * @return
     * @author weizx
     * @date 2018年7月12日 下午2:54:05
     */
    public Map<String, Object> list(SearchVo vo) {
        Map<String, Object> data = new HashMap<String, Object>(1);
        int total = lbbRoleMapper.total(vo.getSearch());
        List<Map<String, Object>> rows = lbbRoleMapper.pagging(vo);
        data.put("total", total);
        data.put("rows", rows);
        return data;
    }

    /**
     * 新增
     *
     * @param vo
     * @author weizx
     * @date 2018年7月12日 下午3:40:38
     */
    public void insert(RoleVo vo) {
        LbbRole role = new LbbRole();
        role.setRoleId(UUIDUtil.S());
        role.setTitle(vo.getTitle());
        role.setRemark(vo.getRemark());
        role.setParentId(vo.getParentId());
        role.setHasDeleted(Booleans.False.name());
        role.setCreateTime(new Date());
        lbbRoleMapper.insertSelective(role);
    }

    /**
     * 更新
     *
     * @param vo
     * @author weizx
     * @date 2018年7月12日 下午3:40:43
     */
    public void update(RoleVo vo) {
        LbbRole role = lbbRoleMapper.getByRoleId(vo.getRoleId());
        if (role == null) {
            return;
        }
        role.setTitle(vo.getTitle());
        role.setRemark(vo.getRemark());
        role.setParentId(vo.getParentId());
        lbbRoleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 获取角色权限
     *
     * @param roleid
     * @return
     * @author weizx
     * @date 2018年7月12日 下午3:55:01
     */
    public List<Map<String, Object>> getAuthorities(String roleid) {
        return authorityMapper.resource(roleid);
    }

    public LbbRole getByRoleId(String roleId) {
        return lbbRoleMapper.getByRoleId(roleId);
    }

    public Long countByParentId(String parentId) {
        LbbRoleExample example = new LbbRoleExample();
        example.or().andParentIdEqualTo(parentId);
        return lbbRoleMapper.countByExample(example);
    }

    public int delete(LbbRole role) {
        role.setHasDeleted("True");
        return lbbRoleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 非指定账号过滤菜单
     * @param menus
     * @param session
     * @return
     */
    public List<Resource> filterMenuByAccount(List<Resource> menus, HttpSession session) {

        try{
            String userId = (String) session.getAttribute("userid");
            if (!TextUtil.isEmpty(userId)) {
                LbbUser lbbUser = lbbUserMapper.getByUserId(userId);
                if(lbbUser!=null){
                    if(!accounts.contains(lbbUser.getUsername())){
                        //当前后台登录账号不为指定可见菜单账号
                        menus.removeAll(urls);
                        for (Resource next : menus) {
                            next.getItems().removeAll(urls);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return menus;
        }
        return menus;
    }
}
